<?php
namespace common\services\lifang;

use linslin\yii2\curl\Curl;
use yii\base\Exception;

class LiFangAir extends LiFang {

    /**
     * 查询飞机票标准商品列表：
        1.辅助接口，确认当前用户被允许使用该标准商品，获取标准商品编号用于货源确认或机票预订；
        2.该商品相对稳定，如已经确认并获取过，可作缓存；
        3.如返回为空，请联系上级确认飞机票商品对接状态以避免下单失败。
     * @param string $pageNo
     * @param string $itemName
     * @param string $itemId
     * @param string $pageSize
     * @return mixed
     * @throws Exception
     */
    public function queryAirShopList($pageNo = '', $itemName ='', $itemId = '', $pageSize = '') {
        $method = 'qianmi.elife.air.items.list';

        $params = [
            "pageNo" => $pageNo,
            "itemName" => $itemName,
            "itemId" => $itemId,
            "pageSize" => $pageSize,
        ];

        $params = $this->getBaseParams($method, $params);

        $curl = new Curl();
        $curl->setGetParams($params);
        $response = $curl->get($this->api);

        return $this->formatResponse($response);
    }

    /**
        查询飞机票商品货源详情(如果已确认当前已对接有效货源，可忽略此步)：
        1.辅助接口，查询飞机票标准商品货源详情，包含供货商，价格等；
        2.如返回为空，请联系上级确认保险商品对接状态以避免下单失败。
     * @param string $itemId
     * @return mixed
     * @throws Exception
     */
    public function queryAirShopInfo($itemId = '') {
        $method = 'qianmi.elife.air.item.detail';

        $params = [
            "itemId" => $itemId,
        ];

        $params = $this->getBaseParams($method, $params);

        $curl = new Curl();
        $curl->setGetParams($params);
        $response = $curl->get($this->api);

        return $this->formatResponse($response);
    }

    /**
     * 查询所有飞机站点信息，信息固定，可缓存客户端用于站点的展示与筛选匹配。
     * @param string $from
     * @param string $to
     * @param string $date
     * @throws \Exception
     */
    public function queryAirStationList() {
        $method = 'qianmi.elife.air.stations.list';

        $params = [

        ];

        $params = $this->getBaseParams($method, $params);

        $curl = new Curl();
        $curl->setGetParams($params);
        $response = $curl->get($this->api);

        return $this->formatResponse($response);
    }

    /**
     * 查询航线列表
        1、以下票面价不包含机场建设费、燃油附加税。
        2、特殊舱位价格变化快，以下特殊舱位价格仅作参考，以订座成功后价格为准。
     * @param string $from
     * @param string $to
     * @param string $date
     * @throws \Exception
     */
    public function queryAirLinesList($from = '', $itemId = '', $date = '',$to ='') {
        $method = 'qianmi.elife.air.lines.list';

        $params = [
            "from" => $from,
            "itemId" => $itemId,
            "date" => $date,
            "to" => $to,
        ];

        $params = $this->getBaseParams($method, $params);

        $curl = new Curl();
        $curl->setGetParams($params);
        $response = $curl->get($this->api);

        return $this->formatResponse($response);
    }

    /**
     * 退订飞机票订单
     * 交易完成，退订订单，支持选择子单退订：
        1.退订订单状态前提：已完成（state-1），已支付（ billstate 1）
        2.“退废票”时间限制：下单当天同时距离起飞时间四小时以上，详细见平台规则。
     * @param string $tradeNo    订单主编号
     * @param string $returnType   退票类型:3-退票
     * @param string $orderNos  订单子单编号集合
     * @throws \Exception
     */
    public function queryAirOrderRefund($tradeNo = '', $returnType ='', $orderNos = '') {
        $method = 'qianmi.elife.air.order.refund';

        $params = [
            "tradeNo" => $tradeNo,
            "returnType" => $returnType,
            "orderNos" => $orderNos
        ];

        $params = $this->getBaseParams($method, $params);

        $curl = new Curl();
        $curl->setGetParams($params);
        $response = $curl->get($this->api);

        return $this->formatResponse($response);
    }

    /**
     * 查询飞机票订单列表
     * 询飞机票订单列表，分页返回
     * @throws \Exception
     */
    public function queryAirOrderList($tradeNo = '',$startTime = '',$endTime = '',$sort = '',$supUserId = '',$pageNo = '',$pageSize = '',$orderStatus = '') {
        $method = 'qianmi.elife.air.orders.list';

        $params = [
            "tradeNo" => $tradeNo,
            "startTime" => $startTime,
            "endTime" => $endTime,
            "sort" => $sort,
            "supUserId" => $supUserId,
            "pageNo" => $pageNo,
            "pageSize" => $pageSize,
            "orderStatus" => $orderStatus,
        ];

        $params = $this->getBaseParams($method, $params);

        $curl = new Curl();
        $curl->setGetParams($params);
        $response = $curl->get($this->api);

        return $this->formatResponse($response);
    }

    /**
     * 查看整笔订单详情
     * @param string $tradeNo   订单编号
     * @throws \Exception
     */
    public function queryAirOrderInfo($tradeNo = '') {
        $method = 'qianmi.elife.air.order.detail';

        $params = [
            "tradeNo" => $tradeNo,
        ];

        $params = $this->getBaseParams($method, $params);

        $curl = new Curl();
        $curl->setGetParams($params);
        $response = $curl->get($this->api);

        return $this->formatResponse($response);
    }

    /**
     * 飞机票订单支付
     * 购票须知
        1、机票相关业务处理时间为8:00-22:00
        2、机票业务不做改签处理
        3、机票支持在线申请退废票和自愿退票，非自愿退票请根据平台规则联系客服处理
        4、出票当日提交"退废票"，具体以航司规定为准
        5、请仔细核对用户身份证号以及姓名，以免造成不必要的损失
        6、行程单(报销凭证)机场提供打印服务
        7、目前不支持飞机票保险货源`
     * @throws \Exception
     */
    public function queryAirOrderPay($seatCode = '',$passagers = '',$itemId = '',$contactName = '',$contactTel = '',
         $date = '' ,$from = '',$to = '',$companyCode = '',$flightNo = '')
    {
        $method = 'qianmi.elife.air.order.payBill';

        $params = [
            "seatCode" => $seatCode,
            "passagers" => $passagers,
            "itemId" => $itemId,
            "contactName" => $contactName,
            "contactTel" => $contactTel,
            "date" => $date,
            "from" => $from,
            "to" => $to,
            "companyCode" => $companyCode,
            "flightNo" => $flightNo,
        ];

        $params = $this->getBaseParams($method, $params);

        $curl = new Curl();
        $curl->setGetParams($params);
        $response = $curl->get($this->api);

        return $this->formatResponse($response);
    }



    private function formatResponse($response = '') {
        $res = json_decode($response, true);

        if (!empty($res['errorToken'])) {
            throw new Exception($res['subErrors'][0]['message']);
        }

        return $res;
    }

    public function DES12306() {

        print_r(self::encrypts(123, "L82V6ZVD6J"));die;
//        print_r(self::decrypts("49baa5ce81c661c804990cfa6087023d", "L82V6ZVD6J"));die;
        $pass = '1';
        $hashed = hash("SHA1", $pass);
        echo $hashed;
    }


    //1. L82V6ZVD6J  字符串获取SHA1摘要字节数组
    //2. 获取的字节数组的前8位作为DES加密的密码
    //3. 摘要的9-16位作为DES加密向量
    //4. 获取DES加密对象，加密方式未DES ，工作模式为CBC，填充模式为PKCS5Padding
    //5. 通过1，2，3步骤设置加密DES加密输出16进制加密结果
    public static function encrypts($string, $key)
    {
        $key = substr(
                openssl_digest(
                    $key, 'sha1', true
                ),
            0,
            16
        );

        // openssl_encrypt 加密不同Mcrypt，对秘钥长度要求，超出16加密结果不变
        $data = openssl_encrypt($string, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);

        $encrypted = strtolower(bin2hex($data));
        return $encrypted;
    }

    public static function decrypts($string, $key)
    {
        $key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
        $decrypted = openssl_decrypt(hex2bin($string), 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
        return $decrypted;
    }
}